import { Component, OnInit, ViewChild, TemplateRef } from '@angular/core';
import { MessageService } from '@iux/live';

@Component({
    templateUrl: './demo3.component.html'
})
export class Demo3 {
    value = 0;
    timer;
    @ViewChild('contentTpl1', { static: false}) contentTpl1: TemplateRef<any>;

    constructor(
        private message: MessageService
    ) { }

    show() {
        this.message.info(this.contentTpl1, {
            lvMessageKey: 'mymessage',
            lvDuration: 0,
            lvOnDestroy: () => { console.log('手动关闭') }
        })

        this.change();
    }

    change() {
        this.timer = setTimeout(() => {
            this.value += Math.floor(Math.random()*5+1);
            if(this.value > 100) {
                this.value = 100;
                this.finish();
                this.timer = null;
            } else {
                this.message.update('mymessage');
                this.change();
            }
        }, 200);
    }

    finish() {
        this.message.update('mymessage', {
            lvType: 'success',
            lvContent: "Loading completed (100%).",
            lvDuration: 2000,
            lvOnDestroy: () => {
                console.log('自动关闭');
                this.value = 0;
            }
        })
    }
}
